$(function () {
        //抽取变量
        var clientPool_datagrid = $("#clientPool_datagrid");
        var clientPool_dialog = $("#clientPool_dialog");
        var clientPool_form = $("#clientPool_form");
        var dispatch_dialog = $("#dispatch_dialog");
        var dispatch_form = $("#dispatch_form");
        //初始化datagrid
        clientPool_datagrid.datagrid({
            fit: true,
            fitColumns: true,
            striped: true,
            url: "/clientPool/list.do",
            pagination: true,
            singleSelect: true,
            rownumbers: true,
            toolbar: "#clientPool_toolbar",
            columns: [[
                {field: 'name', title: '姓名', width: 70, align: 'center'},
                {field: 'traceNum', title: '跟踪次数', width: 60, align: 'center'},
                {field: 'lastTraceTime', title: '最后跟踪时间', width: 100, align: 'center'},
                {field: 'qq', title: 'QQ', width: 80, align: 'center'},
                {field: 'tel', title: '电话', width: 80, align: 'center'},
                {field: 'school', title: '学校', width: 70, align: 'center'},
                {
                    field: 'intentionDegree',
                    title: '意向程度',
                    width: 70,
                    align: 'center',
                    formatter: function (value, row, index) {
                        return value ? value.name : "";
                    }
                },
                {
                    field: 'campus', title: '意向校区', width: 70, align: 'center', formatter: function (value, row, index) {
                        return value ? value.name : "";
                    }
                },
                {
                    field: 'clz', title: '意向班级', width: 70, align: 'center', formatter: function (value, row, index) {
                        return value ? value.name : "";
                    }
                },
                {field: 'remark', title: '摘要', width: 60, align: 'center'}
            ]]
        });
        //初始化dialog
        clientPool_dialog.dialog({
            width: 700,
            height: 500,
            closed: true
        });
        //初始化指派弹窗dialog
        dispatch_dialog.dialog({
            width: 400,
            height: 200,
            closed: true,
            title: "指派窗口",
            buttons: "#dispatch_btns",
        });

        //所有ab标签的方法都交给methodObject来管理
        var methodObjdect = {
            view: function () {
                clientPool_form.form("clear");
                //获取选中行的数据
                var row = clientPool_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "请选择你要编辑的数据", 'info');
                } else {
                    if (row.client) {
                        row["client.id"] = row.client.id;
                    }
                    if (row.state) {
                        row["state.id"] = row.state.id;
                    }
                    if (row.saleman) {
                        row["saleman.id"] = row.saleman.id;
                    }
                    if (row.university) {
                        row["university.id"] = row.university.id;
                    }
                    if (row.clz) {
                        row["clz.id"] = row.clz.id;
                    }
                    if (row.communicationMethod) {
                        row["communicationMethod.id"] = row.communicationMethod.id;
                    }
                    if (row.intentionDegree) {
                        row["intentionDegree.id"] = row.intentionDegree.id;
                    }
                    if (row.tracePurpose) {
                        row["tracePurpose.id"] = row.tracePurpose.id;
                    }
                    //将查询的数据加载进编辑表单中
                    clientPool_form.form("load", row);
                    clientPool_dialog.dialog("setTitle", "查看潜在客户");
                    $("input").prop("disabled", true);
                    clientPool_dialog.dialog("open");
                }
            },
            //刷新操作
            reload: function () {
                clientPool_datagrid.datagrid("reload");
            },
            //移交给我
            joinme: function () {
                //获取选中行的数据
                var row = clientPool_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "请选择你要编辑的数据", 'info');
                    return;
                }
                $.messager.confirm("温馨提示", "确定要移交?", function (r) {
                    if (r) {
                        $.get("/clientPool/join.do", {clientId: row.id}, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    clientPool_datagrid.datagrid("reload");
                                })
                            }
                        }, "json")
                    }
                })
            },
            // 指派给指定咨询人员
            dispatch: function () {
                $("#clientId").combo("readonly", true);
                var client = clientPool_datagrid.datagrid("getSelected");
                console.log(client);
                if (!client) {
                    $.messager.alert("温馨提示", "请选择一行数据!", "info");
                    return;
                }
                dispatch_form.form("clear");
                dispatch_form.form('load', client);
                dispatch_dialog.dialog('open');
            },
            // 提交任务分配表单
            dispatchTo: function () {
                var clientId = $("#clientId").textbox("getValue");
                var salemanId = $("#salemanId").combo("getValue");
                console.log(clientId, salemanId);
                if (!salemanId) {
                    $.messager.alert("温馨提示", "请选择要指派的咨询人员", 'info');
                    return;
                }
                var sendData = {clientId: clientId, salemanId: salemanId};
                $.get("/clientPool/dispatch.do", sendData, function (data) {
                    if (data.success) {
                        $.messager.alert('温馨提示', data.msg, 'info', function () {
                            clientPool_datagrid.datagrid("reload");
                            dispatch_dialog.dialog('close');
                        })
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'info');
                    }
                })

                // dispatch_form.form("submit", {
                //     url: '/clientPool/dispatch.do',
                //     success: function (data) {
                //         data = $.parseJSON(data);
                //         if (data.success) {
                //             $.messager.alert('温馨提示', data.message, 'info', function () {
                //                 clientPool_datagrid.datagrid("reload");
                //                 dispatch_dialog.dialog('close');
                //             })
                //         } else {
                //             $.messager.alert('温馨提示', data.message, 'info');
                //         }
                //     }
                // })
            },
            cancelDispatch: function () {
                dispatch_dialog.dialog("close");
            }
        };
        //methodObject调用方法
        $("[data-cmd]").click(function () {
            var cmd = $(this).data("cmd");
            methodObjdect[cmd]();
        })
    }
);